package com.seecen.controller;

import com.seecen.utils.FileUploadUtil;
import com.seecen.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: SharkBookkeeping-end
 * @description:
 * @version: 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileUploadUtil fileUploadUtil;
    
    @PostMapping("image")
    public Result uploadImage(@RequestParam("image")MultipartFile file) throws IOException {
        try {
            log.info("接收到图片上传请求，文件名: {},大小: {} bytes"
                    , file.getOriginalFilename(), file.getSize());
            String imageUrl = fileUploadUtil.uploadImage(file);
            Map<String, String> result = new HashMap<>();
            result.put("url", imageUrl);
            result.put("name", file.getOriginalFilename());
            result.put("size", String.valueOf(file.getSize()));
            log.info("图片上传成功。访问路径:{}", imageUrl);
            return Result.ok(result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}